package org.example.utils;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class EverydayUttils {

    public String getEveryday() throws IOException {
        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
//        System.out.println("dasda");
        //2. 创建HttpGet请求，并进行相关设置
        HttpGet httpGet = new HttpGet("https://tenapi.cn/v2/yiyan");
//        System.out.println(3);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Mobile Safari/537.36 Edg/85.0.564.68");

//        System.out.println(2);
        //3.发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
//        System.out.println(1);
        //4.判断响应状态码并获取响应数据
        if (response.getStatusLine().getStatusCode() == 200) { //200表示响应成功
//            System.out.println("asd2123");
            return EntityUtils.toString(response.getEntity(), "UTF-8");

        }

        //5.关闭资源
        httpClient.close();
        response.close();
        return "黑白色的夜里，我想看看月亮";
    }

}
